package com.link.base.base.mpconfig.service;

import com.link.base.base.mpconfig.dao.mybatis.mapper.MpMenuMapper;
import com.link.base.base.mpconfig.model.MpMenu;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小程序菜单基础数据维护
 *
 * @author: 闫有霞
 * @date: 2019/12/19
 */
@Service
public class MpMenuServiceImpl extends BasicServiceImpl<MpMenu> implements MpMenuService {

    @Resource
    private MpMenuMapper mpMenuMapper;
    
    @Override
    public BasicMapper<MpMenu> getBasicMapper() {
        return mpMenuMapper;
    }


    /**
     * @createdBy 闫有霞
     * @date 2018/12/7
     * @methods: updateValidFlag
     * @param:  MpMenu entity
     * @return:  void
     * @description: 更新是否有效
     **/

    @Override
    public void updateValidFlag(MpMenu entity) throws Exception {
        mpMenuMapper.updateValidFlag(entity);
    }

    /**
     *@createdBy 闫有霞
     *@date 2018/12/7
     *@methods: deleteAll
     *@param: MpMenu entity
     *@return: void
     *@description: 级联的删除  即删除一级菜单时删除其子菜单
     **/
    @Override
    public void deleteAll(MpMenu entity) throws Exception {
        /*查询该菜单下的子菜单*/
        MpMenu   menu = new   MpMenu();
        menu.setParentMenuId(entity.getId());
        List<MpMenu> subMenuList = mpMenuMapper.queryAll(menu);
        /*如果该菜单下没有子菜单-只删除其自身*/
        if (subMenuList.size() == 0) {
            mpMenuMapper.deleteById(entity);
        } else {
            /*如果该菜单下有子菜单 删除其子菜单并删除其自身*/
            for (MpMenu subMenu : subMenuList) {
                mpMenuMapper.deleteById(subMenu);
            }
            mpMenuMapper.deleteById(entity);
        }
    }
    /**
     *@createdBy 闫有霞
     *@date 2018/12/10
     *@methods: queryMenu
     *@param: MpMenu entity
     *@return: List
     *@description: 查询一级菜单及其对应的二级菜单
     **/

    @Override
    public List<MpMenu> queryMenu(MpMenu entity) throws Exception {
        /*查询所有一级菜单*/
        List<MpMenu> menuList = mpMenuMapper.queryAll(entity);
        /*遍历一级菜单*/
        if (menuList.size() > 0) {
            for (MpMenu item : menuList) {
                MpMenu subMenu = new MpMenu();
                subMenu.setParentMenuId(item.getId());
                subMenu.setMenuGrade("1");
                /*查询一级菜单对应的二级菜单*/
                item.setSubMenuList(mpMenuMapper.queryAll(subMenu));
            }
        }
        return menuList;
    }
}

